
;; Define the COMMON BLOCK
COMMON FFORE_BLOCK,n,rb3,R0,d,R,Z,tau,f_data,sig_f,corr,farlist,do_Tfit,$
   lpstr,rho_hi,rho_h2,rho_star
FORWARD_FUNCTION BGPS_FFORE_MODEL, BGPS_FFORE_LOOPIE, STAR_DENSITY
RESOLVE_ROUTINE, 'BGPS_FFORE', /COMPILE_FULL_FILE, /EITHER

;; Drive the morphological matching for the current list of sources...
restore,'irdc_dist_model/bgps_rb3.sav',/ver
n = n_elements(rb3)

restore,'./irdc_dist_model/irdc_morph_pvec.sav',/ver
restore,'./irdc_dist_model/data/case4_irdc_TdFIT.sav',/ver


;; Define arrays for TEMP_DEPENDENT_VARIABLES
tau    = dblarr(n)
f_data = dblarr(n)
sig_f  = dblarr(n)
do_tfit = 1b
thisnf    = bytarr(n)
fitdist   = fltarr(n)
ffore_vec = fltarr(n)
;; Get galactic params
defsysv, '!MW', exists = exists
IF NOT exists THEN galactic_params 
;; Using !MW parameters -- Goes into COMMON block!
R0 = !MW.R0
d = dindgen(!MW.NBINS*5.)*!MW.BINSIZE + !MW.BINSTART

;; Do the re-suable calculations
GENERATE_LPSTR


res[4] = 20.d

residj = BGPS_FFORE_LOOPIE( res, NEARFAR=thisnf, FFORE_VEC=ffore_vec)

;f_data = f_data > 0.d

print,m4_stat(ffore_vec)
print,m4_stat(f_data)

myps,'./irdc_dist_model/analysis_plots/ffore_histograms.eps',xsize=15,ysize=7.5
plotsym,0,0.5,/fill

multiplot,[4,2],/doyaxis,/doxaxis,ygap=0.04,/rowmajor,xgap=0.03

;; PANEL 1

range = [-0.65,1.05]
hrang = [0,160]
logr  = [0.05,4]
lrang = [65,15]

plothist,f_data,xhst,yhst,bin=0.1,charsize=1.0,thick=3,xr=range,yr=hrang,/yst,$
         xtit='f!ddata!n',ytit='N per 0.1 bin'


multiplot,/doyaxis,/doxaxis 

;; PANEL 2

plot,rb3.l,f_data,psym=8,xr=lrang,/xst,yr=range,/yst,$
     xtit='Galactic Longitude [deg]',ytit='f!ddata!n'

multiplot,/doyaxis,/doxaxis 

;; PANEL 3

plothist,ffore_vec,bin=0.1,charsize=1.0,thick=3,xr=range,yr=hrang,/yst,$
         xtit='f!dmodel!n',ytit='N per 0.1 bin'

multiplot,/doyaxis,/doxaxis 

;; PANEL 4

plot,rb3.l,ffore_vec,psym=8,xr=lrang,/xst,yr=range,/yst,$
     xtit='Galactic Longitude [deg]',ytit='f!dmodel!n'

multiplot,/doyaxis,/doxaxis 

;; PANEL 5

plothist,alog10(rb3.s_peak),xarr,yarr,/noplot,bin=0.1
plot,10.^(xarr)/1.d3,yarr,psym=10,thick=3,xtit='S!d1.1!n [Jy]',/xlog,$
     ytit='N per log(S)=0.1 bin',xr=logr,/xst,/nodata

vline,0.1,thick=3,linestyle=2,color=cgColor('BLU5')
vline,0.3,thick=3,linestyle=2,color=cgColor('GRN4')
vline,1.0,thick=3,linestyle=2,color=cgColor('RED5')

oplot,10.^(xarr)/1.d3,yarr,psym=10,thick=3


multiplot,/doyaxis,/doxaxis 

;; PANEL 6

plot,rb3.l,rb3.s_peak/1.d3,psym=8,xr=lrang,/xst,/ylog,$
     xtit='Galactic Longitude [deg]',ytit='S!d1.1!n [Jy]',yr=logr,/yst

multiplot,/doyaxis,/doxaxis 

;; PANEL 7

plot,rb3.s_peak/1.d3,f_data,psym=8,/xlog,xtit='S!d1.1!n [Jy]',$
     ytit='f!ddata!n',yr=range,/yst,xr=logr,/xst,/nodata

plots,0.1,range[0]
plots,logr[1],1.,/cont,linestyle=3,color=cgColor('Deep Pink'),thick=5

oplot,rb3.s_peak/1.d3,f_data,psym=8


multiplot,/doyaxis,/doxaxis 

;; PANEL 8

plot,rb3.s_peak/1.d3,ffore_vec,psym=8,/xlog,xtit='S!d1.1!n [Jy]',$
     ytit='f!dmodel!n',yr=range,/yst,xr=logr,/xst,/nodata
plots,0.1,range[0]
plots,logr[1],1.,/cont,linestyle=3,color=cgColor('Deep Pink'),thick=5

oplot,rb3.s_peak/1.d3,ffore_vec,psym=8

multiplot,/reset
multiplot,/default
multiplot,/reset

myps,/done


myps,'./irdc_dist_model/analysis_plots/ffore_histograms2.eps',xsize=5
plotsym,0,0.35,/fill

plot,rb3.s_peak/1.d3,f_data,psym=8,/xlog,xtit='S!d1.1!n [Jy]',$
     ytit='f!ddata!n',yr=range,/yst,xr=logr,/xst,/nodata

plots,0.1,range[0]
plots,logr[1],1.,/cont,linestyle=3,color=cgColor('Deep Pink'),thick=5



ind = WHERE(f_data LE 0.)


oploterror,rb3[ind].s_peak/1.d3,f_data[ind],$
           rb3[ind].s_err/1.d3,sig_f[ind],psym=8,errcolor='BLK4'

oplot,rb3.s_peak/1.d3,f_data,psym=8



myps,/done

END
